<?php

namespace App\Http\Services;

use App\Models\CompanyResources;
use App\Models\Enterprise;
use App\Models\Personal;
use Illuminate\Support\Facades\DB;

class OrgService
{
    public function update($item)
    {
        if(!isset($item['set_meal_start']) || empty($item['set_meal_start'])){
            return false;
        }
        if(!isset($item['set_meal_end']) || empty($item['set_meal_end'])){
            return false;
        }

        $data = new Enterprise();
        if(isset($item['id'])){
            $data = Enterprise::find($item['id']);
            if(!$data){
                return false;
            }
        }else{
            $data->create_time = time();
        }

        $MEAL = Enterprise::MEAL;
        $data->user = $item['user'] ?? '';
        $data->account_id = $item['account_id'] ?? 0;
        $data->name = $item['name'] ?? '';
        $data->full_name = $item['full_name'] ?? '';
        $data->company_nature = $item['company_nature'] ?? '';
        $data->scale = $item['scale'] ?? '';
        $data->url = $item['url'] ?? '';
        $data->info = $item['info'] ?? '';
        $data->welfare = $item['welfare'] ?? '';
        $data->contacts = $item['contacts'] ?? '';
        $data->status = $item['status'] ?? 1;
        $data->tell = $item['tell'] ?? '';
        $data->email = $item['email'] ?? '';
        $data->address = $item['address'] ?? '';
        $data->VBRK = $item['VBRK'] ?? '';
        $data->industry = $item['industry'] ?? ''; 
        $data->position = $item['position'] ?? '';
        $data->credit_code = $item['credit_code'] ?? '';
        $data->set_meal_id = $item['set_meal_id'];
        $data->set_meal = $MEAL[$item['set_meal_id']];
        $data->set_meal_start = strtotime($item['set_meal_start']);
        $data->set_meal_end = strtotime($item['set_meal_end']);
        $data->staff_num = $item['staff_num'] ?? 0;
        $data->log = $item['log'] ?? '';
        $data->lat = $item['lat'] ?? '';
        $data->lng = $item['lng'] ?? '';
        $data->tag = $item['tag'] ?? '';
        $data->classify = $item['classify'] ?? '';
        $data->update_time = time();
        $data->save();
        if($data->id){
            return true;
        }
        return  false;
    }

    public function mealEndDate($data){
        try {
            $res = Enterprise::find($data['id']);
            if(empty($data)){
                return false;
            }
            $res->set_meal_end = strtotime($data['set_meal_end']);
            $res->save();
            if(!$res->id){
                return false;
            }
            return true;
        } catch (\Throwable $th) {
            throw $th;
            return  false;
        }
    }

    public function staffNum($data){
        try {
            $res = CompanyResources::where('eid',$data['id'])->first();
            if(empty($data)){
                $res = new CompanyResources();
                $res->eid = $data['id'];
                $res->create_time = time();
            }
            $res->staff_num = $data['staff_num'];
            $res->active_num = $data['active_num'] ?? 0;
            $res->live_num = $data['live_num'] ?? 0;
            $res->update_time = time();
            $res->save();
            if(!$res->id){
                return false;
            }
            return true;
        } catch (\Throwable $th) {
            throw $th;
            return  false;
        }
    }

    public function delete($data){
        try {
            DB::beginTransaction();
            $data->delete();
            Personal::where('es_id',$data->id)->delete();
            DB::commit();
            return true;
        } catch (\Throwable $th) {
            throw $th;
            return  false;
        }
    }
    
}
